package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.login.UserLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.AccountLockVo;
import com.atguigu.tingshu.vo.base.PageVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account")
@SuppressWarnings({"all"})
public class UserAccountApiController {

	@Autowired
	private UserAccountService userAccountService;

	/**
	 * 获取当前登录用户账户可用余额
	 *
	 * @return
	 */
	@UserLogin
	@GetMapping("/userAccount/getAvailableAmount")
	public Result<BigDecimal> getAvailableAmount() {
		Long userId = AuthContextHolder.getUserId();
		BigDecimal money = userAccountService.getAvailableAmount(userId);
		return Result.ok(money);
	}

	@UserLogin(required = true)
	@PostMapping("/userAccount/checkAndLock")
	public Result checkAndDeduct(@RequestBody AccountLockVo accountLockVo){
		Long userId = AuthContextHolder.getUserId();
		accountLockVo.setUserId(userId);
		userAccountService.checkAndLock(accountLockVo);
		return Result.ok();
	}

	@UserLogin
	@GetMapping("/userAccount/findUserRechargePage/{page}/{limit}")
	public Result<Page<UserAccountDetail>> findUserRechargePage(@PathVariable Integer page, @PathVariable Integer limit){
		Page<UserAccountDetail> pageInfo =  new Page<>(page, limit);
		Long userId = AuthContextHolder.getUserId();
		pageInfo = userAccountService.findUserRechargePage(pageInfo,userId, SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT);
		return Result.ok(pageInfo);
	}

	@UserLogin
	@GetMapping("/userAccount/findUserConsumePage/{page}/{limit}")
	public Result<Page<UserAccountDetail>> findUserConsumePage(@PathVariable Integer page, @PathVariable Integer limit){
		Page<UserAccountDetail> pageInfo =  new Page<>(page, limit);
		Long userId = AuthContextHolder.getUserId();
		pageInfo = userAccountService.findUserConsumePage(pageInfo,userId, SystemConstant.ACCOUNT_TRADE_TYPE_MINUS);
		return Result.ok(pageInfo);
	}

}

